<?php  
//if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_controller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
	}
	function index()
	{
		$data['is_first_time'] = true;
		$data['title'] = '';
		$data['id'] = '';
		$this->load->view('header',$data);
		$this->load->view('login_view',$data);
		
	}

	function validate()
	{
		if($this->user_model->validate() ) // if the user's credentials validated...
		{
			$data['email'] = $this->input->post('email');
			$data['is_logged_in'] = true;
			$this->session->set_userdata($data);
			
			/**$this->load_view('homepage_view'); The belowing is better 
			 * beacasue we jump to other controller other than load view
			 * which provides more flexibility
			 */
			redirect('/feed_controller/index');
		}
		else // incorrect username or password
		{
			$data['is_login_success'] = false;
		$data['title'] = '';
		$data['id'] = '';
		$this->load->view('header',$data);
		$this->load->view('login_view',$data);
		$this->load->view('footer',$data);
			
		}
	}

	function signup()
	{
		$data['title'] = 'Register';
		$data['id'] = '';
		$this->load->view('header',$data);
		$this->load->view('signup_view',$data);
	}

	function create_user()
	{
		$this->load->library('form_validation');
		
		// field name, error message, validation rules
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('fullname', 'Full Name', 'trim|required|min_length[2]|max_length[32]');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
		$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
		
		
		if($this->form_validation->run() == FALSE)
		{
			$this->signup();
		}
		else
		{			
			
			if( $query = $this->user_model->create() )
			{
				redirect('/feed_controller/index');
			}
			else
			{
			$data['duplicated_email'] = true;
		$data['title'] = '';
		$data['id'] = '';
		$this->load->view('header',$data);
		$this->load->view('signup_view',$data);
		$this->load->view('footer',$data);
	
			}
		}
	}
	function show_profile()
	{
		$data['title'] = '';
		$data['id'] = '';
		$this->load->view('header',$data);
		$this->load->view('show_profile_view',$data);
		$this->load->view('footer',$data);
	}
	function to_edit_profile()
	{
		$data['title'] = '';
		$data['id'] = '';
		$this->load->view('header',$data);
		$this->load->view('edit_profile_view',$data);
		$this->load->view('footer',$data);
	}
	function edit_profile()
	{
		
	}
	function sign_out()
	{
		$this->session->sess_destroy();
		redirect('/splash_controller');
	}
}